package com.itheima.edu.info.manager.system.controller.impl;

import com.itheima.edu.info.manager.system.controller.ITeacherController;
import com.itheima.edu.info.manager.system.domain.Teacher;
import com.itheima.edu.info.manager.system.factroy.StudentFactory;
import com.itheima.edu.info.manager.system.service.ITeacherService;
import com.itheima.edu.info.manager.system.service.TeacherService;

import java.util.Scanner;

public class TeacherControllerImpl implements ITeacherController {
    private ITeacherService service= StudentFactory.getTeacherService();
    private Scanner sc= new Scanner(System.in);
    @Override
    public void start() {
        //Scanner sc = new Scanner(System.in);
        studentloop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师   5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    //System.out.println("添加");
                    xinZeng();
                    break;
                case 2:
                    //System.out.println("删除");
                    shanChu();
                    break;
                case 3:
                    //System.out.println("修改");
                    xiuGai();
                    break;
                case 4:
                    //System.out.println("查询");
                    chaXunSuoYou();
                    break;
                case 5:
                    System.out.println("谢谢使用老师管理系统");
                    // return;
                    break studentloop;
                default:
                    System.out.println("输入有误");
                    break;

            }

        }
    }

    @Override
    public void xinZeng() {
        String id;
        while(true){
            System.out.println("录入工号");
            id= sc.next();
            boolean b = service.panDuan(id);
            if(b){
                //重新录
                System.out.println("再录");
            }else{
                break;
            }
        }
        Teacher teacher = luRuLaoShi(id);
        service.xinZeng(teacher);
        System.out.println("新增成功");
    }

    private Teacher luRuLaoShi(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //创建老师对象 封装数据
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return  teacher;
    }

    @Override
    public void shanChu() {

        String id = luRuId();
        service.shanChu(id);
        System.out.println("删除成功");

    }

    private String luRuId() {

        while (true){
            System.out.println("请输入工号");
            String id = sc.next();
            boolean b = service.panDuan(id);
            if(b){
                return id;
            }else{

                System.out.println("再录");
            }
        }
    }

    @Override
    public void xiuGai() {
        String id = luRuId();
        Teacher teacher = luRuLaoShi(id);
        service.xiuGai(id,teacher);
        System.out.println("修改成功");
    }

    @Override
    public void chaXunSuoYou() {
        Teacher[] teachers = service.chaXunSuoYou();
        if(teachers==null){
            System.out.println("没有");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        //4.循环数组 打印数据
        for (int i = 0; i < teachers.length; i++) {
            //stus[i]
            Teacher t = teachers[i];
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "岁\t" + t.getBirthday());

            }

        }

    }
}
